What is @aws-cdk/aws-kms?
@aws-cdk/aws-kms is an AWS CDK library that allows you to define and manage AWS Key Management Service (KMS) resources in your AWS infrastructure as code. It provides constructs for creating and managing KMS keys, aliases, and grants, enabling secure encryption and decryption of data.
What are @aws-cdk/aws-kms's main functionalities?
Create a KMS Key
This code sample demonstrates how to create a new KMS key with key rotation enabled and an alias using the AWS CDK.
const cdk = require('@aws-cdk/core');
const kms = require('@aws-cdk/aws-kms');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const key = new kms.Key(stack, 'MyKey', {
enableKeyRotation: true,
alias: 'alias/my-key'
});
app.synth();
Create a KMS Alias
This code sample demonstrates how to create a new KMS alias that points to an existing KMS key using the AWS CDK.
const cdk = require('@aws-cdk/core');
const kms = require('@aws-cdk/aws-kms');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const key = new kms.Key(stack, 'MyKey');
const alias = new kms.Alias(stack, 'MyAlias', {
aliasName: 'alias/my-alias',
targetKey: key
});
app.synth();
Grant Permissions to a KMS Key
This code sample demonstrates how to grant encrypt and decrypt permissions to an IAM user for a KMS key using the AWS CDK.
const cdk = require('@aws-cdk/core');
const kms = require('@aws-cdk/aws-kms');
const iam = require('@aws-cdk/aws-iam');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const key = new kms.Key(stack, 'MyKey');
const user = new iam.User(stack, 'MyUser');
key.grantEncryptDecrypt(user);
app.synth();
Other packages similar to @aws-cdk/aws-kms
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, which provides a comprehensive set of tools for interacting with AWS services, including KMS. Unlike @aws-cdk/aws-kms, which is used for defining infrastructure as code, aws-sdk is used for making API calls to AWS services directly from your application code.
serverless
The serverless package is a framework for building and deploying serverless applications on AWS and other cloud providers. It includes support for managing AWS KMS keys as part of your serverless infrastructure. While it provides similar functionality for managing KMS keys, it is more focused on serverless architectures compared to the broader infrastructure management capabilities of @aws-cdk/aws-kms.
terraform
Terraform is an open-source infrastructure as code tool that allows you to define and manage cloud resources, including AWS KMS keys, using a declarative configuration language. It provides similar functionality to @aws-cdk/aws-kms but uses a different syntax and approach to infrastructure management.
AWS KMS Construct Library
Defines a KMS key:
new EncryptionKey(this, 'MyKey', {
enableKeyRotation: true
});
Add a couple of aliases:
const key = new EncryptionKey(this, 'MyKey');
key.addAlias('alias/foo');
key.addAlias('alias/bar');
Importing and exporting keys
To use a KMS key that is not defined within this stack, use the
EncryptionKey.import(parent, name, ref)
factory method:
const key = EncryptionKey.import(this, 'MyImportedKey', {
keyArn: new KeyArn('arn:aws:...')
});
key.addAlias('alias/foo');
To export a key from a stack and import it in another stack, use key.export
which returns an EncryptionKeyRef
, which can later be used to import:
const myKey = new EncryptionKey(stackA, 'MyKey');
const myKeyRef = myKey.export();
const myKeyImported = EncryptionKey.import(stackB, 'MyKeyImported', myKeyRef);
Note that a call to .addToPolicy(statement)
on myKeyImported
will not have
an affect on the key's policy because it is not owned by your stack. The call
will be a no-op.
0.9.2 (2018-09-20)
NOTICE: This release includes a framework-wide breaking change which changes the type of all the string resource attributes across the framework. Instead of using strong-types that extend cdk.Token
(such as QueueArn
, TopicName
, etc), we now represent all these attributes as normal string
s, and codify the tokens into the string (using the feature introduced in #168).
Furthermore, the cdk.Arn
type has been removed. In order to format/parse ARNs, use the static methods on cdk.ArnUtils
.
See motivation and discussion in #695.
Breaking Changes
- cfn2ts: use stringified tokens for resource attributes instead of strong types (#712) (6508f78), closes #518 #695 #744
- aws-dynamodb: Attribute type for keys, changes the signature of the
addPartitionKey
and addSortKey
methods to be consistent across the board. (#720) (e6cc189) - aws-codebuild: fix typo "priviledged" -> "privileged
Bug Fixes
Features
- aws-apigateway: new API Gateway Construct Library (#665) (b0f3857)
- aws-cdk: detect presence of EC2 credentials (#724) (8e8c295), closes #702 #130
- aws-codepipeline: make the Stage insertion API in CodePipeline more flexible (#460) (d182818)
- aws-codepipeline: new "Pipeline#addStage" convenience method (#647) (25c9fa0)
- aws-rds: add support for parameter groups (#729) (2541508), closes #719
- docs: add documentation for CDK toolkit plugings (#733) (965b918)
- dependencies: upgrade to jsii 0.7.6